<?php
/**
 * 需求任务表qa_item
 * String id
 * String need_id【谁的需求】
 * String task_id【谁的任务】
 * String title【标题】
 * String site【网站】
 * String content【详情】
 * String priority【优先级：0为COOL，1为WARM，2为HOT】
 * String statu【状态：0为未开始READ，1为进行中DOING，2为已完成DONE】
 * String memo【备注】
 * String evaluate【评价】
 * String add_time【发布需求的时间】
 * String asign_time【指派任务的时间】
 * String start_time【开始任务的时间】
 * String end_time【完成任务的时间】
 */
class App_Model_QaItem extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('QaItem');
	}

	public function add($row)
	{
		return $this->table->insert($row);
	}

	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->delete($where);
	}

	public function getOne($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}
	
	public function update($row, $id)
	{
		$db = $this->table->getAdapter();
		$where=array();
		$where[] = $db->quoteInto('id = ?', $id);
		$where=join(' AND ',$where);
	
		return $this->table->update($row, $where);
	}
	
	public function getQaItem($fieldArray)
	{
		$select = $this->table->select();
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$select->where("$field = ?",$value);
			}
		}
		$select->order('add_time asc');
		return $this->table->fetchAll($select);
	}
	
	public function getQaItemByDate($starttime,$endtime,$taskid)
	{
		$select = $this->table->select();
		if(!empty($taskid)){
			$select->where('task_id = ?',$taskid);
		}
		if(!empty($starttime)){
			$select->where('end_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('end_time < ?',$endtime);
		}
		
		return $this->table->fetchAll($select);
	}
	
	public function listAll($needid,$taskid,$title,$site,$statu,$starttime,$endtime,$page=1,$limit=20)
	{
		$select = $this->table->select();
		if(!empty($needid)){
			$select->where('need_id = ?',$needid);
		}
		if(!empty($taskid)){
			$select->where('task_id = ?',$taskid);
		}
		if(!empty($title)){
			$slice = explode(" ", trim($title));
			foreach($slice as $v){
				$select->where('title like ?','%'.$v.'%');
			}
		}
		if(!empty($site)){
			$select->where('site = ?',$site);
		}
		if(!($statu=='')){
			$select->where('statu = ?',$statu);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
		
		$select->order('add_time desc');
        return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($needid,$taskid,$title,$site,$statu,$starttime,$endtime)
	{
		$where="where 1 ";
		
		if(!empty($needid)){
			$where.=" and need_id='$needid'";
		}
		if(!empty($taskid)){
			$where.=" and task_id='$taskid'";
		}
		if(!empty($title)){
			$arr = explode(' ', $title);//如果输入的title搜索有空格隔开，则分割字符串然后再生成语句
			foreach ($arr as $v){
				$where.=" and title like '%$v%'";
			}
		}
		if(!empty($site)){
			$where.=" and site='$site'";
		}
		if(!empty($statu)){
			$where.=" and statu='$statu'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from qa_item ".$where); 
	}
}